package com.plian.system.service.yth.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plian.Tools.CollectionUtil;
import com.plian.Tools.DateUtil;
import com.plian.Tools.StringUtil;
import com.plian.system.constant.sys.FormTypeConstant;
import com.plian.system.entity.yth.nsgl.*;
import com.plian.system.mapper.yth.nsgl.*;
import com.plian.system.service.yth.IProblemMainService;
import com.plian.system.vo.fr.internalauditmanagement.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 数据采集-内审工作年度工作计划备案主表实体类
 *
 * @author baiyang
 * @since 2023-03-14
 */
@Slf4j
@Service
//@DS("slave_1")
public class ProblemMainServiceImpl extends ServiceImpl<ProblemMainMapper, ProblemMain> implements IProblemMainService {

    private final String YTH_DATASOURCE_TYPE = "3";

    @Autowired
    private ProblemAuditRecordMapper problemAuditRecordMapper;

    @Autowired
    private ProblemEffectListMapper problemEffectListMapper;

    @Autowired
    private ProblemListMapper problemListMapper;

    @Autowired
    private ProblemProcessStatusMapper problemProcessStatusMapper;

    @Autowired
    private ProblemRectificationMapper problemRectificationMapper;

    @Autowired
    private ProblemRectifyTargetMapper problemRectifyTargetMapper;


    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
//    @DS("slave_1")
    @Override
    public List<IamAuditRectifyVO> getSyncData(Date startTime, Date endTime) {
        List<IamAuditRectifyVO> iamAuditRectifyVOS = new ArrayList<>();
        List<ProblemMain> ythAuditPlans = list(new LambdaQueryWrapper<ProblemMain>()
                .gt(Optional.ofNullable(startTime).isPresent(), ProblemMain::getCreateTime, startTime)
                .le(Optional.ofNullable(endTime).isPresent(), ProblemMain::getCreateTime, endTime)
        );
        if (CollectionUtil.isNotEmpty(ythAuditPlans)) {
            iamAuditRectifyVOS = conversion(ythAuditPlans);
        }
        return iamAuditRectifyVOS;
    }

    /**
     * @param ythAuditPlans
     * @return
     */
    private List<IamAuditRectifyVO> conversion(List<ProblemMain> ythAuditPlans) {
        List<IamAuditRectifyVO> iamAuditRectifyVOS = new ArrayList<>();


        for (ProblemMain problemMain : ythAuditPlans) {

            IamAuditRectifyVO iamAuditRectifyVO = new IamAuditRectifyVO();
            iamAuditRectifyVO.setUnitId(Objects.toString(problemMain.getReportOrgId(), null));
            iamAuditRectifyVO.setUnitName(Objects.toString(problemMain.getReportOrgName(), null));
            iamAuditRectifyVO.setFillingYear(Objects.toString(problemMain.getProblemDate(), null));
            iamAuditRectifyVO.setRectifyStatus(Objects.toString(problemMain.getAuditProgress(), null));
            iamAuditRectifyVO.setCreateCompanyId(Objects.toString(problemMain.getReportOrgId(), null));
            iamAuditRectifyVO.setCreateUser(Objects.toString(problemMain.getReportUserId(), null));
            iamAuditRectifyVO.setStatus(0);
            iamAuditRectifyVO.setCreateTime(new Date());
            iamAuditRectifyVO.setDataSource(YTH_DATASOURCE_TYPE);
            iamAuditRectifyVO.setDataSourceId(problemMain.getId().toString());
            iamAuditRectifyVO.setFormType(String.valueOf(FormTypeConstant.IAM_AUDIT_PROBLEM_RECTIFY_LIST));
            //问题清单
            ProblemList problemList = problemListMapper.selectOne(new LambdaQueryWrapper<ProblemList>().eq(ProblemList::getMainId, problemMain.getId()));
            if (Optional.ofNullable(problemList).isPresent()) {
                IamRectifyIssureListVO iamRectifyIssureListVO = new IamRectifyIssureListVO();
                iamRectifyIssureListVO.setUnitId(problemList.getProblemUnitId());
                iamRectifyIssureListVO.setUnitName(problemList.getProblemUnitNm());
                iamRectifyIssureListVO.setIssureDomain(problemList.getProblemDomain());
                iamRectifyIssureListVO.setIssureNature(transIssureNature(Objects.toString(problemList.getProblemNature(), null)));
                iamRectifyIssureListVO.setIssureExpress(problemList.getProblemDescribe());
                iamRectifyIssureListVO.setFindIssueDate(problemList.getProblemDate() != null ? DateUtil.parse(problemList.getProblemDate(), DateUtil.PATTERN_DATE) : null);
                iamRectifyIssureListVO.setQualitativeBasis(problemList.getBasis());
                iamRectifyIssureListVO.setAuditProcessOpinion(problemList.getAuditOpinion());
                iamRectifyIssureListVO.setIssureSourceReport(problemList.getAuditReportNm());
                iamRectifyIssureListVO.setAuditReportNum(problemList.getAuditReportSymbol());
                iamRectifyIssureListVO.setIssureMatters(problemList.getSpecificMatter());
                iamRectifyIssureListVO.setRectifyType(transRectifyType(Objects.toString(problemList.getRectifyType(), null)));
                iamRectifyIssureListVO.setRemark(problemList.getRemarks());
                iamRectifyIssureListVO.setDataSource(YTH_DATASOURCE_TYPE);
                iamRectifyIssureListVO.setDataSourceId(problemList.getId().toString());
                iamAuditRectifyVO.setIssureList(iamRectifyIssureListVO);
            }
            //目标清单
            ProblemRectifyTarget problemRectifyTarget = problemRectifyTargetMapper.selectOne(new LambdaQueryWrapper<ProblemRectifyTarget>().eq(ProblemRectifyTarget::getMainId, problemMain.getId()));
            if (Optional.ofNullable(problemRectifyTarget).isPresent()) {
                IamRectifyTargetListVO rectifyTargetListVO = new IamRectifyTargetListVO();
                rectifyTargetListVO.setRectifyTarget(problemRectifyTarget.getRectifyTarget());
                rectifyTargetListVO.setRectifyMoney(Objects.toString(problemRectifyTarget.getRectifyAmount(), null));
                rectifyTargetListVO.setRectifyMeasure(problemRectifyTarget.getRectifyMeasure());
                rectifyTargetListVO.setRectifyLeadDept(problemRectifyTarget.getMainPerson());
                rectifyTargetListVO.setRectifyLeadDeptMobile(problemRectifyTarget.getMainPhone());
                rectifyTargetListVO.setRectifyAssistDept(problemRectifyTarget.getCooperatePerson());
                rectifyTargetListVO.setRectifyAssistDeptMobile(problemRectifyTarget.getCooperatePhone());
                rectifyTargetListVO.setRectifyDutyUnit(problemRectifyTarget.getDutyUnit());
                rectifyTargetListVO.setRectifyDutyUnitMobile(problemRectifyTarget.getDutyPhone());
                rectifyTargetListVO.setUrgeLeadDutyUnit(problemRectifyTarget.getSupervisionPerson());
                rectifyTargetListVO.setDefiniteDeptImplements(problemRectifyTarget.getExecutePerson());
                rectifyTargetListVO.setDataSource(YTH_DATASOURCE_TYPE);
                rectifyTargetListVO.setDataSourceId(problemRectifyTarget.getId().toString());
                iamAuditRectifyVO.setTargetList(rectifyTargetListVO);
            }

            //成效清单
            ProblemEffectList problemEffectList = problemEffectListMapper.selectOne(new LambdaQueryWrapper<ProblemEffectList>().eq(ProblemEffectList::getMainId, problemMain.getId()));
            if (Optional.ofNullable(problemEffectList).isPresent()) {
                IamRectifyReformListVO reformListVO = new IamRectifyReformListVO();
                reformListVO.setRectifyResult(problemEffectList.getRectifyResult());
                reformListVO.setRectifyProcessFile(problemEffectList.getRectifyRecord());
                reformListVO.setRectifySourceMeasure(problemEffectList.getRectifyReason());
                reformListVO.setPartyProcessType(problemEffectList.getPartyDisposeType());
                reformListVO.setPartyProcessNum(problemEffectList.getPartyDisposeNum());
                reformListVO.setGovernmentAffairsNum(problemEffectList.getPoliticsDisposeNum());
                reformListVO.setGovernmentAffairsType(problemEffectList.getPoliticsDisposeType());
                reformListVO.setFormDutyType(problemEffectList.getImputationType());
                reformListVO.setFormDutyNum(problemEffectList.getImputationNum());
                reformListVO.setUpdateRegulationName(problemEffectList.getUpdSystemNm());
                reformListVO.setUpdateRegulationNum(problemEffectList.getUpdSystemNum());
                reformListVO.setNewRegulationName(problemEffectList.getNewSystemNm());
                reformListVO.setNewRegulationNum(problemEffectList.getNewSystemNum());
                reformListVO.setManaSystemName(problemEffectList.getManagementNm());
                reformListVO.setManaSystemNum(problemEffectList.getManagementNum());
                reformListVO.setOtherNum(problemEffectList.getOtherNum());
                reformListVO.setOtherName(problemEffectList.getOtherNm());
                reformListVO.setRemark(problemEffectList.getRemarks());
                reformListVO.setDataSource(YTH_DATASOURCE_TYPE);
                reformListVO.setDataSourceId(problemEffectList.getId().toString());
                iamAuditRectifyVO.setReformList(reformListVO);
            }

            //整改认定
            ProblemRectification problemRectification = problemRectificationMapper.selectOne(new LambdaQueryWrapper<ProblemRectification>().eq(ProblemRectification::getMainId, problemMain.getId()));
            if (Optional.ofNullable(problemRectification).isPresent()) {
                IamRectifyIdentifyListVO iamRectifyIdentifyListVO = new IamRectifyIdentifyListVO();
                iamRectifyIdentifyListVO.setDutyUnitIdentify(problemRectification.getSelfInfo());
                iamRectifyIdentifyListVO.setLeadUnitIdentify(problemRectification.getPreliminaryInfo());
                iamRectifyIdentifyListVO.setAuditDeptIdentify(problemRectification.getAuditInfo());
                iamRectifyIdentifyListVO.setRectifyDeptIdentify(problemRectification.getReviewInfo());
                iamRectifyIdentifyListVO.setRemark(problemRectification.getRemarks());
                iamRectifyIdentifyListVO.setDataSource(YTH_DATASOURCE_TYPE);
                iamRectifyIdentifyListVO.setDataSourceId(problemRectification.getId().toString());
                iamAuditRectifyVO.setIdentifyList(iamRectifyIdentifyListVO);
            }
            iamAuditRectifyVOS.add(iamAuditRectifyVO);
        }

        return iamAuditRectifyVOS;
    }

    /**
     * 问题性质
     *
     * @param type
     * @return
     */
    private  String transIssureNature(String type) {
        if (StringUtil.isNotBlank(type)) {
            switch (type) {
                case "1":
                    return "160001";
                case "2":
                    return "160002";
                case "3":
                    return "160003";
                case "4":
                    return "160004";
                case "5":
                    return "160005";
                case "6":
                    return "160006";
                case "7":
                    return "160007";
                case "8":
                    return "160008";
                case "9":
                    return "160009";
                case "10":
                    return "160010";
                case "11":
                    return "160011";
                case "12":
                    return "160012";
                case "13":
                    return "160013";
                case "14":
                    return "160014";
                case "15":
                    return "160015";
                case "16":
                    return "160016";
                case "17":
                    return "160017";
                case "18":
                    return "160018";
                case "19":
                    return "160019";
                case "20":
                    return "160020";
                case "21":
                    return "160021";
                case "22":
                    return "160022";
                case "23":
                    return "160023";
                case "24":
                    return "160024";
                case "25":
                    return "160025";
                case "26":
                    return "160026";
                default:
                    return null;
            }
        }
        return null;
    }

    /**
     * 整改类型
     *
     * @param type
     * @return
     */
    private String transRectifyType(String type) {
        if (StringUtil.isNotBlank(type)) {
            switch (type) {
                case "1":
                    return "159001";
                case "2":
                    return "159002";
                case "3":
                    return "159003";
                default:
                    return null;
            }
        }
        return null;
    }

}
